package mbu;

import junit.framework.Assert;
import robocode.control.events.BattleCompletedEvent;
import robocode.control.events.TurnEndedEvent;
import robocode.control.snapshot.IRobotSnapshot;
import robocode.control.testing.RobotTestBed;
import robocode.control.BattlefieldSpecification;

/**
 * Tests that StairShooter passes the horizontal line of the battle field.
 * 
 * @author Michael Uranaka
 * 
 */
public class TestRobotPassesHorizontalLine extends RobotTestBed {

  // Global variables.
  boolean horizontalPass = false;
  String robotName = "";

  /**
   * Specifies that SittingDuck and StairShootoer are to be matched up in this test case.
   * 
   * @return The comma-delimited list of robots in this match.
   */
  @Override
  public String getRobotNames() {
    return "sample.SittingDuck,mbu.StairShooter";
  }

  /**
   * This test runs for 1 round.
   * 
   * @return The number of rounds.
   */
  @Override
  public int getNumRounds() {
    return 5;
  }

  /**
   * After each turn, check if the robot passed to horizontal line.
   * 
   * @param event Info about the current state of the battle.
   */
  @Override
  public void onTurnEnded(TurnEndedEvent event) {

    // Select StairShooter form snapshot.
    IRobotSnapshot robot = event.getTurnSnapshot().getRobots()[1];
    // Save the robot name.
    robotName = robot.getName();

    // Get StairShooters Y coordinate.
    double currentY = robot.getY();

    // Get the Y coordinate of the middle of the field.
    BattlefieldSpecification b = new BattlefieldSpecification();
    int fieldHeight = b.getHeight();
    int midField = fieldHeight / 2;

    // If StairShooters Y coordinate is greater than the middle, the horizontal line was crossed.
    if ((int) currentY > midField) {
      horizontalPass = true;
    }
  }

  /**
   * Returns a comma or space separated list like: x1,y1,heading1, x2,y2,heading2, which are the
   * coordinates and heading of robot #1 and #2. So "0,0,180, 50,80,270" means that robot #1 has
   * position (0,0) and heading 180, and robot #2 has position (50,80) and heading 270.
   * 
   * Override this method to explicitly specify the initial positions for your test cases.
   * 
   * Defaults to null, which means that the initial positions are determined randomly. Since battles
   * are deterministic by default, the initial positions are randomly chosen but will always be the
   * same each time you run the test case.
   * 
   * @return The list of initial positions.
   */
  @Override
  public String getInitialPositions() {
    return "0,0,0,200,200,0";
  }

  /**
   * After the battle, check if the robot passed the horizontal line.
   * 
   * @param event Details about the completed battle.
   */
  @Override
  public void onBattleCompleted(BattleCompletedEvent event) {
    Assert.assertEquals("Check robot name",robotName, "mbu.StairShooter*");
    Assert.assertEquals("Check horizontal pass", horizontalPass, true);

  }

}
